<?php
namespace TennisTournament\Model;

use Doctrine\ORM\Mapping as ORM;
use TennisTournament\Model\GameEnum;
use TennisTournament\Model\Match;
use \InvalidArgumentException;

/** @ORM\Entity */
class Result {

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     * @var integer
     */
    protected $id;

    /**
     * @ORM\Column(type="smallint", name="host_score")
     * @var integer
     */
    protected $hostScore;

    /**
     * @ORM\Column(type="smallint", name="guest_score")
     * @var integer
     */
    protected $guestScore;

    /**
     * Value pointed from {@link GameEnum}
     * @ORM\Column
     * @var string
     */
    protected $game;

    /**
     * @ORM\ManyToOne(targetEntity="Match", inversedBy="results")
     * @ORM\JoinColumn(name="match_id", referencedColumnName="id")
     * @var Match
     */
    protected $match;

    /**
     * @param string $game
     * @throws InvalidArgumentException
     */
    public function setGame($game) {
        if (!in_array($game, array(GameEnum::FIRST, GameEnum::SECOND, GameEnum::THIRD))) {
            throw new InvalidArgumentException("Invalid winner type");
        }
        $this->game = $game;
    }

    /**
     * @return string
     */
    public function getGame() {
        return $this->game;
    }

    /**
     * @param int $guestScore
     */
    public function setGuestScore($guestScore) {
        $this->guestScore = $guestScore;
    }

    /**
     * @return int
     */
    public function getGuestScore() {
        return $this->guestScore;
    }

    /**
     * @param int $hostScore
     */
    public function setHostScore($hostScore) {
        $this->hostScore = $hostScore;
    }

    /**
     * @return int
     */
    public function getHostScore() {
        return $this->hostScore;
    }

    /**
     * @param int $id
     */
    public function setId($id) {
        $this->id = $id;
    }

    /**
     * @return int
     */
    public function getId() {
        return $this->id;
    }

    /**
     * @param \TennisTournament\Model\Match $match
     */
    public function setMatch($match) {
        $this->match = $match;
    }

    /**
     * @return \TennisTournament\Model\Match
     */
    public function getMatch() {
        return $this->match;
    }

    /**
     * Convert a Result into a associative array
     * @return array
     */
    public function toArray() {
        return array(
            'id'            =>  $this->getId(),
            'matchId'       =>  $this->getMatch()->getId(),
            'guestScore'    =>  $this->getGuestScore(),
            'hostScore'     =>  $this->getHostScore(),
            'game'         =>  $this->getGame()
        );
    }

}